<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />

          <el-table-column label="操作" align="center" fixed="right" width="120">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.checkType=='0'&&scope.row.status != '1'"
                v-hasPermi="['device:spareCheck:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <span v-else class="el-button--text button_grey">编辑</span>
              <el-button
                v-if="scope.row.status != '1'"
                v-hasPermi="['device:spareCheck:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
              <span v-else class="el-button--text button_grey">删除</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { list, remove, getInfo } from '@/api/equipmentManage/sparePart/inventory'
import { list as listIsNotEmpty } from '@/api/equipmentManage/sparePart/warehouse'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
import Detail from './detail.vue'
import { getDictSelects } from '@/api/common/common'
import ShowDetailOld from '@/components/JLINK2/common/ShowDetailOld' // 查看页面
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '备件盘点',
      pageData: {
        companyName: undefined,
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      total: 1, // 数据一共有多少条
      mainId: 'id',
      tableHeight: '0',
      tableData: [],
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '盘点单号' },
        { prop: 'checkTypeName', label: '盘点方式', canOrder: false },
        { prop: 'checkRealName', label: '盘点人员', canOrder: false },
        { prop: 'checkTime', label: '盘点时间' },
        { prop: 'deviceSpareWarehouseName', label: '仓库', canOrder: false },
        { prop: 'remark', label: '备注', canOrder: false }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        { prop: 'statusList', label: '状态', type: 'select-multiple', optionsName: 'statusOptions' },
        // { prop: 'code', label: '盘点单号', type: 'input' },
        // { prop: 'checkRealName', label: '盘点人员', type: 'input' },
        { prop: 'checkTime', label: '盘点时间', type: 'time' },
        { prop: 'deviceSpareWarehouseId', label: '仓库', type: 'select', optionsName: 'statusOptionsWarehouse' },
        { prop: 'checkType', label: '盘点方式', type: 'select', optionsName: 'statusOptionsWay' }
      ],

      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'device:spareCheck:add' },
        { label: '批量删除', perm: 'device:spareCheck:delete' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [ // 选择组件数据源
        { value: '0', label: '待盘点' },
        { value: '2', label: '盘点中' },
        { value: '1', label: '已完成' }
      ],
      statusOptionsWay: [
        { value: undefined, label: '全部' },
        { value: '0', label: '普通盘点' },
        { value: '1', label: '扫码盘点' }
      ],
      statusOptionsWarehouse: [],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '盘点单号', key: 'code' },
          { type: 'form', name: '仓库', key: 'deviceSpareWarehouseName' },
          { type: 'form', name: '盘点方式', key: 'checkTypeName' },
          { type: 'form', name: '盘点人员', key: 'checkRealName' },
          { type: 'form', name: '盘点时间', key: 'checkTime' },
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          { type: 'table', key: 'data', columns: [
            { type: 'column', name: '备件编码', key: 'spareCode', minWidth: '120' },
            { type: 'column', name: '备件名称', key: 'spareName', minWidth: '100' },
            { type: 'column', name: '规格型号', key: 'spareModel', minWidth: '100' },
            { type: 'column', name: '备件供应商', key: 'supplierName', minWidth: '100' },
            { type: 'column', name: '备件单位', key: 'numberUnitLabel', minWidth: '100' },
            { type: 'column', name: '批号', key: 'batch', minWidth: '100' },
            { type: 'column', name: '库存数量', key: 'number', minWidth: this.SETTINGS.settings_decimalDigits_length },
            { type: 'column', name: '实盘数量', key: 'checkNumber', minWidth: this.SETTINGS.settings_decimalDigits_length },
            { type: 'column', name: '差异数量', key: 'differenceNumber', minWidth: this.SETTINGS.settings_decimalDigits_length }
          ] }
        ]
      }
    }
  },
  computed: {
    // 分页器
    selectedNum() {
      return this.$refs.table ? this.$refs.table.selection.length : 0
    }
  },
  created() {
    this.loadWarehouseList()
    this.getDictOptions()
  },
  mounted() {

  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.showDetail(row)
          break
      }
    },
    showDetail(row) {
      getInfo(row[this.mainId]).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetailOld, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看备件盘点', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    getDictOptions() {
      getDictSelects('inventory_status').then(res => {
        this.dictOptions = res.data
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    loadWarehouseList() {
      listIsNotEmpty().then(res => {
        console.log(res)
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.name
        })
        res.data.unshift({ value: undefined, label: '全部' })
        this.statusOptionsWarehouse = res.data
      })
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['1320px', '700px'],
        title: '新增备件盘点', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId],
            row: row
          }// props
        },
        area: ['1320px', '700px'],
        title: '编辑备件盘点', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    detail(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: Detail, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId],
            row: row
          }// props
        },
        area: ['1320px', '700px'],
        title: '查看备件盘点', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    }
  }
}
</script>

<style lang="less" scoped>
 .main_main{
  .form_padding{
    /deep/ .el-form-item{
      margin-right: 0px;
    }
    /deep/ .el-form-item__label{
      width: 130px !important;
    }
    /deep/ .el-form-item__content{
      width: 290px !important;
    }
    .remark{
      /deep/ .el-form-item__label{
        width: 130px !important;
      }
      /deep/ .el-form-item__content{
        width: 720px !important;
      }
    }
  }

}
.expand_container{
  .el-form-item{
    margin-bottom: 10px;
  }
  .expand_file{
    margin-bottom: 0px;
  }

  /deep/ .el-form-item__label{
    width: 130px !important;
  }
  /deep/ .el-form-item__content{
    width: 800px !important;
  }
}
</style>
